package com.zjj.distributed.zk.curator.caches;

import com.zjj.distributed.zk.curator.CuratorBaseOperations;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.junit.Test;

/**
 * @author zhanglei.zjj
 * @description NodeCache 对某一个节点进行监听
 * @date 2023/6/20 20:26
 */
@Slf4j
public class NodeCacheTest extends CuratorBaseOperations {
    public static final String NODE_CACHE = "/node-cache";

    @Test
    public void testNodeCacheTest() throws Exception {

        createIfNeed(NODE_CACHE, "zjj");
        NodeCache nodeCache = new NodeCache(curatorFramework, NODE_CACHE);
        nodeCache.getListenable().addListener(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                log.info("{} path nodeChanged: ", NODE_CACHE);
                printNodeData(NODE_CACHE);
            }
        });

        nodeCache.start();
        Thread.sleep(Integer.MAX_VALUE);
    }
}
